﻿using System;
using System.Collections;
using System.Collections.Generic;
using Affinity.Core.Validation;

namespace Affinity.Core.Exceptions
{
    public class ExceptionWrapper : Exception
    {
        public class Parameters
        {
            public IDictionary Data { get; set; }
            public string Message { get; set; }
            public string StackTrace { get; set; }
            public Exception WrappedException { get; set; }
        }

        public ExceptionWrapper(Parameters parameters)
            : base(parameters == null ? null : parameters.Message, parameters == null ? null : parameters.WrappedException)
        {
            Assert.NotNull(parameters, "parameters");
            _parameters = parameters;
        }

        public override IDictionary Data
        {
            get
            {
                if (_parameters.Data != null) return _parameters.Data;
                if (_parameters.WrappedException != null) return _parameters.WrappedException.Data;
                return new Dictionary<object, object>();
            }
        }

        public override string Message
        {
            get
            {
                if (_parameters.Message != null) return _parameters.Message;
                if (_parameters.WrappedException != null) return _parameters.WrappedException.Message;
                return string.Empty;
            }
        }

        public override string StackTrace
        {
            get
            {
                if (_parameters.StackTrace != null) return _parameters.StackTrace;
                if (_parameters.WrappedException != null) return _parameters.WrappedException.StackTrace;
                return null;
            }
        }

        private readonly Parameters _parameters;
    }
}
